package com.neusoft.neumooc.dao;

import java.util.List;
import org.springframework.stereotype.Repository;
import com.neusoft.core.common.BaseDAO;
import com.neusoft.core.common.exception.DAOException;
import com.neusoft.neumooc.vo.BackMenuVO;

@Repository
public class BackMenuDAOImpl extends BaseDAO implements BackMenuDAO {

    private static final String SQL_GET_ALL_MENUS = 
        "SELECT menuId, menuName, menuNameUs, parentId, linkType, openType, link, seq " +
        "FROM cw_menus ORDER BY seq ASC";
        
    private static final String SQL_GET_PARENT_MENUS = 
        "SELECT menuId, menuName, menuNameUs, parentId, linkType, openType, link, seq " +
        "FROM cw_menus WHERE parentId IS NULL ORDER BY seq ASC";
        
    private static final String SQL_GET_CHILD_MENUS = 
        "SELECT menuId, menuName, menuNameUs, parentId, linkType, openType, link, seq " +
        "FROM cw_menus WHERE parentId = :parentId ORDER BY seq ASC";
        
    private static final String SQL_ADD_MENU = 
        "INSERT INTO cw_menus(menuId, menuName, menuNameUs, parentId, linkType, openType, link, seq) " +
        "VALUES(:menuId, :menuName, :menuNameUs, NULL, :linkType, :openType, :link, :seq)";
        
    private static final String SQL_ADD_CHILD_MENU = 
        "INSERT INTO cw_menus(menuId, menuName, menuNameUs, parentId, linkType, openType, link, seq) " +
        "VALUES(:menuId, :menuName, :menuNameUs, :parentId, :linkType, :openType, :link, :seq)";
        
    private static final String SQL_UPDATE_MENU = 
        "UPDATE cw_menus SET menuName = :menuName, menuNameUs = :menuNameUs, " +
        "parentId = :parentId, linkType = :linkType, openType = :openType, " +
        "link = :link, seq = :seq WHERE menuId = :menuId";
        
    private static final String SQL_DELETE_MENU = 
        "DELETE FROM cw_menus WHERE menuId = :menuId";
        
    private static final String SQL_GET_MENU_BY_ID = 
        "SELECT menuId, menuName, menuNameUs, parentId, linkType, openType, link, seq " +
        "FROM cw_menus WHERE menuId = :menuId";

    @Override
    public List<BackMenuVO> getAllMenus() throws DAOException {
        try {
            return this.queryForBeanList(SQL_GET_ALL_MENUS, BackMenuVO.class);
        } catch (Exception e) {
            log.error("获取所有菜单列表失败", e);
            throw new DAOException("获取所有菜单列表失败", e);
        }
    }

    @Override
    public List<BackMenuVO> getParentMenus() throws DAOException {
        try {
            return this.queryForBeanList(SQL_GET_PARENT_MENUS, BackMenuVO.class);
        } catch (Exception e) {
            log.error("获取一级菜单列表失败", e);
            throw new DAOException("获取一级菜单列表失败", e);
        }
    }

    @Override
    public List<BackMenuVO> getChildMenus(String parentId) throws DAOException {
        try {
            BackMenuVO param = new BackMenuVO();
            param.setParentId(parentId);
            return this.queryForBeanListByBean(SQL_GET_CHILD_MENUS, BackMenuVO.class, param);
        } catch (Exception e) {
            log.error("获取子菜单列表失败", e);
            throw new DAOException("获取子菜单列表失败", e);
        }
    }

    @Override
    public boolean addMenu(BackMenuVO menu) throws DAOException {
        try {
            String sql = (menu.getParentId() == null || menu.getParentId().trim().isEmpty()) 
                      ? SQL_ADD_MENU 
                      : SQL_ADD_CHILD_MENU;
            return this.update(sql, menu) > 0;
        } catch (Exception e) {
            log.error("添加菜单失败", e);
            throw new DAOException("添加菜单失败", e);
        }
    }

    @Override
    public boolean updateMenu(BackMenuVO menu) throws DAOException {
        try {
            return this.update(SQL_UPDATE_MENU, menu) > 0;
        } catch (Exception e) {
            log.error("更新菜单失败", e);
            throw new DAOException("更新菜单失败", e);
        }
    }

    @Override
    public boolean deleteMenu(String menuId) throws DAOException {
        try {
            BackMenuVO param = new BackMenuVO();
            param.setMenuId(menuId);
            return this.update(SQL_DELETE_MENU, param) > 0;
        } catch (Exception e) {
            log.error("删除菜单失败", e);
            throw new DAOException("删除菜单失败", e);
        }
    }

    @Override
    public BackMenuVO getMenuById(String menuId) throws DAOException {
        try {
            BackMenuVO param = new BackMenuVO();
            param.setMenuId(menuId);
            return this.queryForBeanByBean(SQL_GET_MENU_BY_ID, BackMenuVO.class, param);
        } catch (Exception e) {
            log.error("根据ID获取菜单失败", e);
            throw new DAOException("根据ID获取菜单失败", e);
        }
    }
} 